package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户列表查询
     * @param userDto
     * @return
     */
    @GetMapping
    public Result list(UserDto userDto){
        log.info("用户列表查询:{}",userDto);
        PageResult<User> pageResult =userService.list(userDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        log.info("用户添加:{}",user);
        userService.add(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("用户删除:{}",ids);
        userService.delete(ids);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody User user){
        log.info("用户修改:{}",user);
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        User user =userService.getById(id);
        return Result.success(user);
    }

    @GetMapping("/role/{roleLabel}")
    public  Result getByRole(@PathVariable String roleLabel){
        log.info("查询角色:{}",roleLabel);
        List<User> list = userService.getByRole(roleLabel);
        return Result.success(list);
    }
}
